<?php
if(!defined('INSIDE')){
	exit("Access Denied");
}

class util{
	public static function redirect($url){
		header("HTTP/1.1 301 Moved Permanently");
		header("location: $url");
	}
	
	public static function line($str){
		if(substr_compare($str, "\n", -1, 1) != 0){
			$str.="\n";
		}
		return $str;
	}
	
	public static function getgpc($k, $type='GP') {
		$type = strtoupper($type);
		switch($type) {
			case 'G': $var = &$_GET; break;
			case 'P': $var = &$_POST; break;
			case 'C': $var = &$_COOKIE; break;
			default:
				if(isset($_POST[$k])) {
					$var = &$_POST;
				} else {
					$var = &$_GET;
				}
				break;
		}
		return isset($var[$k]) ? $var[$k] : NULL;
	}
	
	public static function file_abs($file_type, $file, $ext_name=null){
		$location = SITE_ROOT.'/'.self::file_rel($file_type, $file, $ext_name);
		if(file_exists($location)){
			return $location;
		}
		else{
			return NULL;
		}
	}
	
	public static function file_rel($file_type, $file, $ext_name=null){
		$location = '';
		$extnm = $ext_name;
		$type = null;
		$sub_type = null;
		if(strpos($file_type, ":") === false){
			$type=$file_type;
		}
		else{
			// template:[tpl img css js item]
			// module: no sub type
			// class: no sub type
			// static: no sub type
			$arr=explode(":", $file_type);
			$type = $arr[0];
			$sub_type = $arr[1];
		}
		
		switch(strtolower($type)){
			case "template":
			case "tpl":
				$arr = explode("/", trim($file));
				$tmp_str = '';
				foreach($arr as $item){
					$tmp_str.='/'.$item;
				}
				global $_GVAR;
				if(empty($sub_type) || strcmp($sub_type, "tpl") == 0){
					$location='template'.$tmp_str;
					if(empty($extnm)){
						$extnm='tpl.html';
					}
				}
				else if(strcmp($sub_type, "img") == 0){
					$location='template/'.$_GVAR['theme'].'/static/img/'.$tmp_str;
				}
				else if(strcmp($sub_type, "css") == 0){
				  $location='template/'.$_GVAR['theme'].'/static/css/'.$tmp_str;
				  if(empty($extnm)){
				    $extnm='css';
				  }
				}
				else if(strcmp($sub_type, "js") == 0){
				  $location='template/'.$_GVAR['theme'].'/static/js/'.$tmp_str;
				  if(empty($extnm)){
				    $extnm='js';
				  }
				}
				else if(strcmp($sub_type, "item") == 0){
					$location='template/'.$_GVAR['theme'].$tmp_str;
				}
				else
					return "unknown_sub_type";
				break;
			case "module":
			case "mod":
				// $app/$mod
				$arr = explode("/", trim($file));
				$location='source/module/'.$arr[0].'/'.$arr[1];
				if(empty($extnm)){
					$extnm='php';
				}
				break;
			case "action":
			case "act":
				// $app/$act
				$arr = explode("/", trim($file));
				$location='source/action/'.$arr[0].'/'.$arr[1];
				if(empty($extnm)){
					$extnm='php';
				}
				break;
			case "controller":
			case "ctrl":
				// $app/$act
				$location='source/controller/'.trim($file);
				if(empty($extnm)){
					$extnm='php';
				}
				break;
			case "class":
				// $class
				$location='source/class/'.$file;
				if(empty($extnm)){
					$extnm='php';
				}
				break;
			case "static":
				// $path/$filename.$ext_name
				$arr = explode("/", trim($file));
				$tmp_str = '';
				foreach ($arr as $item){
					$tmp_str.='/'.$item;
				}
				$location='static'.$tmp_str;
				break;
			case "app":
				$location=$file;
				if(empty($extnm)){
					$extnm='php';
				}
				break;
			case "language":
			case "lang":
				global $_GVAR;
				if(strcmp($file, "theme") == 0){
					$location = 'template/'.$_GVAR['theme'].'/language.'.$_GVAR['language'];
				}
				else{
					$location = 'language/'.$_GVAR['language'].'/'.$file;
				}
				if(empty($extnm)){
					$extnm='txt';  //chs.txt, cht.txt, en.txt
				}
				break;
			default:
				$location='';
				break;
		}
		if(!empty($extnm)){
			$location=$location.'.'.$extnm;
		}
// 		echo $location."<br/>";
		return $location;
	}
	
	public static function getcachetplfilename($theme, $language, $tplfile){
		// eg: $tplfile = "home/index"
		return $language.'_'.$theme.'_'.str_replace(array("/", "\\"), "_", $tplfile).'.tpl.php';
	}
	
	public static function geturl($ctrl, $action){
		$url = $ctrl.'.php';
		if(!empty($action)){
			$url.='?act='.$mod;
		}
		return $url;
	}
	
	public static function loadmod($mod, $item){
		$modfile = util::file_abs('mod', $mod.'/'.$item);
		if(!empty($modfile)){
			include $modfile;
		}
		else{
			exit('Missing File: '.$mod.'/'.$item);
		}
	}
	
	public static function file_content($file){
		$handle = @fopen($file, "r");
		if ($handle) {
			$ret = '';
			$filesize = filesize($file);
			$ret.=fread($handle, $filesize>0?$filesize:1);
			@fclose($handle);
			return $ret;
		}
		else{
			return NULL;
		}
	}
	
	public static function compress_str($str){
		$ret = '';
		$arr = explode("\n", $str);
		foreach($arr as $line){
			$ret.=str_replace(array("\r", "\n", "\t"), "", trim($line));
		}
		$ret.="\n";
		return $ret;
	}
	
	public static function log_test($str){
		$fp = fopen('c:/util.log', 'a');
		fwrite($fp, $str."\n");
		fclose($fp);
	}
}
?>